﻿using System;
using System.IO;
using java.io;
using weka.core;

namespace MLSharp.Weka
{
	/// <summary>
	/// Provides a unified means of loading <see cref="IDataSet"/>s from
	/// all supported file formats.
	/// </summary>
	public static class DataSetLoader
	{
		/// <summary>
		/// Loads a dataset from the specified filename.
		/// </summary>
		/// <param name="filename">The path to the file to load.</param>
		/// <returns>The dataset.</returns>
		/// <remarks>
		/// This wraps loaders for the various supported dataset types.
		/// </remarks>
		public static IDataSet Load(string filename)
		{
			string extension = Path.GetExtension(filename).ToLowerInvariant();

			switch (extension)
			{
				case ".xls":
					return new ExcelDataSet(filename);
				case ".arff":
					Instances instances = new Instances(new BufferedReader(new FileReader(filename)));

					return instances.ToDataSet();
				default:
					throw new NotSupportedException("No loader found for the specified dataset.");
			}
		}
	}
}
